package com.chao.auth.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @Description:   
 * @className: JwtTokenEnhancer 
 * @author fu wenchao
 * @date 2022/1/24 22:45 
 * @company:西安博达软件股份有限公司
 * @copyright: Copyright (c) 2022
 * @version V1.0 
 */
@Component
public class JwtTokenEnhancer implements TokenEnhancer {

    /**
      * @description 把一些用户基础信息设置到jwt中
      * @author Rookie
      * @date 2022/1/25 22:53
      * @param accessToken
      * @param authentication
      * @return org.springframework.security.oauth2.common.OAuth2AccessToken
      */
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        Map<String, Object> hashMap = new HashMap<>();
        if (authentication.getPrincipal() instanceof UserDetails){
            UserDetails securityUser = (UserDetails) authentication.getPrincipal();
            hashMap.put("username",securityUser.getUsername());
        }
        hashMap.put("tag","test");
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(hashMap);
        return accessToken;
    }
}
